import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
import { AuthService } from './auth.service';
import { RegisterDto } from './dto/register.dto';

import { AuthGuard } from '@nestjs/passport';
import { LoginDto } from './dto/login.dto';
import { JwtService } from '@nestjs/jwt';

@Controller('auth')
export class AuthController {

    constructor(
        private jwtService: JwtService,
        private readonly authService: AuthService
    ) {}

    @Post('register')
    @ApiOperation({summary: '注册'})
    register(@Body() registeredUser: RegisterDto){
        return this.authService.regisUser(registeredUser)
    }

    @Post('login')
    @ApiOperation({summary: '登录'})
    @UseGuards(AuthGuard('local'))
    login(@Body() dto: LoginDto, @Req() req){
        return {
            token: this.jwtService.sign(String(req.user.id)),
            userInfo: req.user
        }
    }

    @Get('user')
    @ApiOperation({summary: '获取个人信息'})
    @UseGuards(AuthGuard('jwt'))
    @ApiBearerAuth()
    user(@Req() req){
        return req.user;
    }
}
